/* Copyright (c) 2011 by Mani Brundha Kattamuri, Haritha Subramanyam
 *
 * Project Info:  http://code.google.com/p/andriod-travelapp-csc780/wiki/Design
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */
package com.example.TravelApp.search.advanced;

import java.util.ArrayList;

import com.example.TravelApp.R;
import com.example.TravelApp.search.common.GPlaceObjectActivity;
import com.example.TravelApp.search.common.PlaceObject;
import com.example.TravelApp.search.common.PlacesSharedPreference;
import com.example.TravelApp.search.common.SerializablePlaceObject;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Parcelable;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * This activity is called when the user selects a particular place among the
 * list of places. The layout is described in file
 * <code>res/layout/interest_layout.xml</code>. This activity displays details
 * of a place.
 * 
 * @author Haritha Subramanyam, Mani Brundha
 * 
 */

public class AdvancedEachSubOptionDetailActivity extends ListActivity {
	private static TextView heading;
	private ArrayList<PlaceObject> list;
	ArrayAdapter<PlaceObject> adapter;
	private String place;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.interest_layout_save);
		heading = (TextView) findViewById(R.id.header);
		Typeface font = Typeface.createFromAsset(getAssets(),
				"Chantelli_Antiqua.ttf");
		heading.setTypeface(font);
		Intent myIntent = getIntent();
		this.list = myIntent.getParcelableArrayListExtra("placesList");
		place = (String) myIntent.getCharSequenceExtra("Place");
		adapter = new GPlaceObjectActivity(this, this.list);

		setListAdapter(adapter);
		ListView listView = getListView();
		/**
		 * Implements an action listener for collecting place the user selected.
		 * Listens to the user's click and starts the map activity which shows
		 * the route to the selected place
		 */
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getBaseContext(),
						MapSrcToDestRouteActivity.class)
						.putParcelableArrayListExtra("placesList",
								(ArrayList<? extends Parcelable>) list)
						.putExtra("position", position)
						.putExtra("Place", place);
				startActivityForResult(intent, 0);
			}

		});
	}

	/**
	 * Called after onRestoreInstanceState(Bundle), onRestart(), or onPause(),
	 * for your activity to start interacting with the user.
	 */
	public void onResume() {
		((GPlaceObjectActivity) adapter).notifyDataSetChanged();
		setListAdapter(getListAdapter());
		super.onResume();
	}

	/**
	 * Called whenever MapSrcToDestRouteActivity terminates.
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		int p = data.getExtras().getInt("pos");
		boolean x = data.getExtras().getBoolean("visit");
		list.get(p).setIsVisited(x);
		if (resultCode == RESULT_OK) {

			onResume();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * Invoked whenever the save button is clicked. Saves the entire list of
	 * options provided to the user along with information on whether a place is
	 * visited or not. This current state is then saved using preferences so
	 * that user can continue with visiting places in the saved list at a later
	 * point of time
	 */
	public void onClick(View v) {
		String savePlaces = SerializablePlaceObject.objectToString(list);
		PlacesSharedPreference.saveStringObject(getBaseContext(),
				"mySavedPlaces", savePlaces);
	}

}
